package com.creative.accountsdk.holder;

import android.app.Dialog;
import android.util.Log;
import android.view.ViewGroup;

import com.creative.accountsdk.AccountActivity;
import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.AuthorizeListener;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.ThirdLoginProtocol;
import cz.msebera.android.httpclient.Header;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;

import cn.sharesdk.framework.Platform;

/**
 * Created by yinjialiang on 15-6-23.
 */
public abstract class BaseHolder implements AuthorizeListener{
    protected AccountActivity activity;
    protected ViewGroup viewRoot;
    private   Dialog dialogAuthorize;
    private   final static String TAG = BaseHolder.class.getSimpleName();
    protected  Dialog progress;
    public BaseHolder()
    {}

    public void init(AccountActivity activity, ViewGroup viewRoot)
    {
        this.activity = activity;
        this.viewRoot = viewRoot;
        initViews();
        progress = DialogUtils.createProgress(activity);
    }

    protected void showAuthorizeDialog()
    {
        dialogAuthorize = DialogUtils.createProgress(activity);
        dialogAuthorize.show();
    }
    protected abstract void initViews();
    public abstract int getLayoutResID();

    public ViewGroup getViewRoot() {
        return viewRoot;
    }

    @Override
    public void authorizeSuccess(int platformID, Platform platform) {
        Log.d(TAG, "authorizeSuccess");
        String userId = platform.getDb().getUserId();
        String token = platform.getDb().getToken();
        String userName = platform.getDb().getUserName();
        String userIcon = platform.getDb().getUserIcon();
        String userGender = platform.getDb().getUserGender();
//                StringBuilder sb = new StringBuilder();
//                sb.append("platformID = ").append(platformID);
//                sb.append(" ,userName = ").append(userName);
//                sb.append(" ,userGender = ").append(userGender);
//                Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();

//                Toast.makeText(this, "existAndGuest = " + isUserExistAndGuest(), Toast.LENGTH_LONG).show();

        if(progress.isShowing()){
            progress.dismiss();
        }
        progress.show();
        final ThirdLoginProtocol protocol = new ThirdLoginProtocol(platformID, userId, token, userName);
        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, activity)) {
                    Log.d(TAG, "third login success");
                    UserData.getInstance().init(protocol.jsonUser);
                    UserData.getInstance().write(activity);
                    activity.finish();
                    OnLoginListener loginListener = AccountSdk.getInstance().getLoginListener();
                    if(loginListener != null)
                    {
                        loginListener.onComplete(protocol.jsonUser);
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                progress.cancel();

            }
        });

    }

    @Override
    public void authorizeCancel() {
        progress.cancel();
        dialogAuthorize.cancel();
    }

    @Override
    public void authorizeError() {
        progress.cancel();
        dialogAuthorize.cancel();
    }

    public void resizeBigger()
    {}

    public void resizeSmaller()
    {}
}
